const jwtUtil = require('../utils/jwtUtils')
module.exports=class student_dao extends require('../model/student_mod'){
  /**
   * 获取所有通知和数量
   * @param req
   * @param res
   * @returns {Promise<void>}
   */
  static async getNotice(req,res){
    let verify=await jwtUtil.verifysync(req.query.token,globalKey)
    let u_classes=verify.classes
    let pageNum=req.query.pageNum
    let currPage=req.query.currPage
    let data = await this.getNoticeMod(u_classes,pageNum,currPage)
    let total=await this.getNoticeTotal(u_classes)
    res.send({data,total:total[0].count})
  }

  /**
   * 获取我的已读通知
   * @param req
   * @param res
   * @returns {Promise<void>}
   */
  static async getNoticeRead(req,res){
    let verify=await jwtUtil.verifysync(req.query.token,globalKey)
    let u_id=verify.id
    let data=await this.getNoticeReadMod(u_id)
    res.send(data)
  }

  /**
   * 未读转已读
   * @param req
   * @param res
   * @returns {Promise<void>}
   */
  static async goReadily(req,res){
      let verify=await jwtUtil.verifysync(req.query.token,globalKey)
      let u_id=verify.id
      let n_id=req.query.n_id
      let result=await this.goReadilyMod(u_id,n_id)
      res.send(result)
  }

  /**
   * 健康报表
   * @param req
   * @param res
   * @returns {Promise<void>}
   */
  static async setHealty(req,res){
    let body=req.body
    let token=body.token
    let name=body.name
    let classes=body.classes
    let living=body.living
    let healthy=body.healthy
    let leaveout=body.leaveout
    let nucleic=body.nucleic
    let phone=body.phone
    let verify=await jwtUtil.verifysync(token,globalKey)
    let u_id=verify.id
    let data=await this.setHealtyMod(u_id,name,classes,living,healthy,leaveout,nucleic,phone)
    res.send(data)
  }

  /**
   * 分页获取当天的报表与数量
   * @param req
   * @param res
   * @returns {Promise<void>}
   */
  static async getHealtyNowaday(req,res){
    let date=new Date();
    let Month=""
    if(date.getMonth()+1<10) Month="0"+String((date.getMonth()+1))
    else Month=(date.getMonth()+1)+""
    let newDate=""+date.getFullYear()+Month+date.getDate()
    let lastDate=""+date.getFullYear()+Month+(date.getDate()+1)
    let currPage=req.query.currPage
    let pageNum=req.query.pageNum
    let data=await this.getHealtyNowadayMod(newDate,lastDate,currPage,pageNum)
    let total=await this.getHealtyNowadayTotal(newDate,lastDate)
    res.send({data,total:total[0].count})
  }

  /**
   * 获取当天某用户的报表
   * @param req
   * @param res
   * @returns {Promise<void>}
   */
  static async getHealtyNowadaByid(req,res){
    let date=new Date();
    let Month=""
    if(date.getMonth()+1<10) Month="0"+String((date.getMonth()+1))
    else Month=(date.getMonth()+1)+""
    let newDate=""+date.getFullYear()+Month+date.getDate()
    let lastDate=""+date.getFullYear()+Month+(date.getDate()+1)
    let verify=await jwtUtil.verifysync(req.query.token,globalKey)
    let u_id=verify.id
    let data=await this.getHealtyNowadaByidMod(u_id,newDate,lastDate)
    res.send(data)
  }

  /**
   * 根据u_id获取用户信息
   * @param req
   * @param res
   * @returns {Promise<void>}
   */
  static async getuserinfo(req,res){
    let verify=await jwtUtil.verifysync(req.query.token,globalKey)
    let u_id=verify.id
    let data=await this.getuserinfoMod(u_id)
    res.send(data)
  }

  /**
   * 请假申请
   * @param req
   * @param res
   * @returns {Promise<void>}
   */
  static async setleave(req,res){
    let verify=await jwtUtil.verifysync(req.body.token,globalKey)
    let u_id=verify.id
    let classes=verify.classes
    let reason=req.body.reason
    let leavetype=req.body.leavetype
    let starttime=req.body.starttime
    let endtime=req.body.endtime
    let result=await this.setleaveMod(u_id,classes,reason,leavetype,starttime,endtime)
    res.send(result)
    // if (result.length>1)
    // res.send('申请成功')
    // else
    //   res.send('申请失败')
  }

  /**
   * 获取用户自己的所有请假申请
   * @param req
   * @param res
   * @returns {Promise<void>}
   */
  static async getleavebyid(req,res){
    let verify=await jwtUtil.verifysync(req.query.token,globalKey)
    let u_id=verify.id
    let data=await this.getleavebyidMod(u_id)
    res.send(data)
  }
}
